var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    agentUser: {
      userId:0,
      userName: '',
      gender: 0,
      userLevelId: 0,
      mobile: ''
    },
    agentUserId: 0
  },
  bindinputMobile(event) {
    let agentUser = this.data.agentUser; 
    agentUser.mobile = event.detail.value;
    this.setData({
      agentUser: agentUser
    });
  },
  bindinputName(event) {
    let agentUser = this.data.agentUser;
    agentUser.userName = event.detail.value;
    this.setData({
      agentUser: agentUser
    });
  },
  bindinputGender (event){
    let agentUser = this.data.agentUser;
    agentUser.gender = event.detail.value;
    this.setData({
      agentUser: agentUser
    });
  },
  bindinputuserLevelId (event){
    let agentUser = this.data.agentUser;
    agentUser.userLevelId = event.detail.value;
    this.setData({
      agentUser: agentUser
    });
  },

  getAgentUserDetail() {
    let that = this;
    util.request(api.AgentUserDetail, { id: that.data.agentUserId }).then(function (res) {
      if (res.errno === 0) {
        if(res.data){
            that.setData({
              agentUser: res.data
            });
        }
      }
    });
  },


  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.id) {
      this.setData({
        agentUserId: options.id
      });
    }
    this.getAgentUserDetail();
  },

  onReady: function () {

  },
  cancelAgentUser(){
    wx.navigateBack({
      url: '/pages/ucenter/agentUser/agentUser',
    })
  },
  saveAgentUser(){
    let agentUser = this.data.agentUser;

    if (agentUser.userName == '') {
      util.showErrorToast('请输入姓名');

      return false;
    }

    if (agentUser.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }

    // if (agentUser.gender == 0) {
    //   util.showErrorToast('请输入性别');
    //   return false;
    // }

    if (agentUser.userLevelId == 0) {
      util.showErrorToast('请输入用户级别');
      return false;
    }

    let that = this;
    util.request(api.AgentUserSave, { 
      userId: agentUser.userId,
      userName: agentUser.userName,
      mobile: agentUser.mobile,
      gender: agentUser.gender,
      userLevelId: agentUser.userLevelId,
    }, 'POST', 'application/json').then(function (res) {
      if (res.errno === 0) {
        wx.navigateBack({
          url: '/pages/ucenter/agentUser/agentUser',
        })
      }
    });

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
})